package com.jfirer.validator.constraint.validator;

import java.util.Calendar;
import java.util.Date;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.Past;

public class PastValidator implements ConstraintValidator<Past, Object>
{
    
    @Override
    public void initialize(Past constraintAnnotation)
    {
    }
    
    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context)
    {
        if (value == null)
        {
            return true;
        }
        if (value instanceof Date)
        {
            Date date = (Date) value;
            return System.currentTimeMillis() > date.getTime();
        }
        else if (value instanceof Calendar)
        {
            Calendar calendar = (Calendar) value;
            return System.currentTimeMillis() > calendar.getTimeInMillis();
        }
        else
        {
            throw new UnsupportedOperationException("只支持Date和Calendar");
        }
    }
    
}
